package com.wondertek.poms.service.controller;

import com.wondertek.poms.core.service.ISystemConfigService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author gaozhengxing gaozhenghang@wondertek.com.cn
 * @date 2021/1/4 14:30
 * @description TODO
 */
@Api(value = "FileController", description = "上传文件控件")
@RestController
@RequestMapping(value="/file")
public class FileController {

    @Autowired
    private ISystemConfigService systemConfigService;

    @PostMapping(value = "/fileUpload")
    public String fileUpload(@RequestParam(value = "file") MultipartFile file,
         @RequestParam(value = "type")int type,@RequestParam(value = "content")String content,Model model) {
        if (file.isEmpty()) {
            System.out.println("文件为空");
        }
        // 文件名
        String fileName = file.getOriginalFilename();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        // 上传后的路径
        String filePath= systemConfigService.findAllBykey("IMAGE_PATH").getValue()+ File.separator + "temp" + File.separator+ sdf.format(new Date()) + File.separator + fileName;
        //测试使用
//        filePath = "d://"+filePath;
        File dest = new File(filePath);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("getAbsolutePath:"+dest.getAbsolutePath());
        System.out.println("getPath:"+dest.getPath());
        return dest.getPath();
    }

}
